﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ReadyApps.Data;
using ReadyApps.Wiki.Templates;

namespace ReadyApps.Wiki.Views
{
    class WikiRevisionListView : IWikiView
    {
        #region Variables and Constants

        private const string REVISION_STATUS_CURRENT = "Current";
        private const string REVISION_STATUS_PAST = "Past";

        private int pageID;
        private IWikiDataProvider dataProvider;
        private WikiControlSettings controlSettings;

        #endregion

        #region Events

        private event EventHandler normalViewButtonClick;
        public event EventHandler NormalViewButtonClick
        {
            add { normalViewButtonClick += value; }
            remove { normalViewButtonClick -= value; }
        }

        private event EventHandler<ViewRevisionEventArgs> viewRevisionButtonClick;
        public event EventHandler<ViewRevisionEventArgs> ViewRevisionButtonClick
        {
            add { viewRevisionButtonClick += value; }
            remove { viewRevisionButtonClick -= value; }
        }

        #endregion

        #region Constructor

        public WikiRevisionListView(int wikiPageID, IWikiDataProvider wikiDataProvider, WikiControlSettings wikiControlSettings)
        {
            if (wikiDataProvider == null)
            {
                throw new ArgumentNullException("wikiDataProvider");
            }
            pageID = wikiPageID;
            dataProvider = wikiDataProvider;
            controlSettings = wikiControlSettings;
        }

        #endregion

        #region Public Methods

        public void BuildControlTree(ControlCollection controls, bool raiseEvents)
        {
            Control container = new Control();

            HtmlGenericControl paragraph = new HtmlGenericControl("p");
            paragraph.InnerText = "Select a revision to view the page content from that version.";
            container.Controls.Add(paragraph);

            HtmlTable table = new HtmlTable();
            table.Rows.Add(BuildHeaderRow());
            foreach (var revision in dataProvider.GetPageRevisionList(pageID))
            {
                table.Rows.Add(BuildRevisionRow(revision));
            }
            container.Controls.Add(table);

            WikiControlButton normalViewButton = WikiControlButton.GetFromTemplate(controlSettings.ButtonTemplate);
            normalViewButton.ButtonControl.Text = "Return";
            normalViewButton.ButtonControl.Click += normalViewButtonClick;
            container.Controls.Add(normalViewButton.RootControl);

            controls.Clear();
            controls.Add(container);
        }

        #endregion

        #region Private Methods

        private HtmlTableRow BuildHeaderRow()
        {
            HtmlTableRow header = new HtmlTableRow();
            
            header.Cells.Add(new HtmlTableCell());
            header.Cells.Add(new HtmlTableCell() { InnerText = "Revision Number" });
            header.Cells.Add(new HtmlTableCell() { InnerText = "Page Title" });
            header.Cells.Add(new HtmlTableCell() { InnerText = "Revision Date" });
            header.Cells.Add(new HtmlTableCell() { InnerText = "Status" });

            return header;
        }

        private HtmlTableRow BuildRevisionRow(WikiPageRevisionData revisionData)
        {
            HtmlTableRow row = new HtmlTableRow();

            LinkButton lb = new LinkButton();
            lb.Text = "View";
            if (revisionData.RevisionStatus == REVISION_STATUS_CURRENT)
            {
                lb.Click += normalViewButtonClick;
            }
            else
            {
                lb.CommandArgument = revisionData.RevisionNumber.ToString();
                lb.Click += new EventHandler(lb_Click);
            }
            HtmlTableCell commandCell = new HtmlTableCell();
            commandCell.Controls.Add(lb);

            row.Cells.Add(commandCell);
            row.Cells.Add(new HtmlTableCell() { InnerText = revisionData.RevisionNumber.ToString() });
            row.Cells.Add(new HtmlTableCell() { InnerText = revisionData.PageTitle });
            row.Cells.Add(new HtmlTableCell() { InnerText = revisionData.RevisionDate.ToString("f") });
            row.Cells.Add(new HtmlTableCell() { InnerText = revisionData.RevisionStatus });

            return row;
        }

        #endregion

        #region Event Handlers

        private void lb_Click(object sender, EventArgs e)
        {
            if (viewRevisionButtonClick != null)
            {
                int revisionNumber = int.Parse(((LinkButton)sender).CommandArgument);
                viewRevisionButtonClick(this, new ViewRevisionEventArgs(revisionNumber));
            }
        }

        #endregion
    }
}
